﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpaceGame
{
    public static class CollisionManager
    {
        public static void ManageCollisions(IEnumerable<GameObject> objectsToCheck)
        {
            foreach (var firstInstance in objectsToCheck)
            {
                if (!(firstInstance is ICollidable))
                {
                    continue;
                }

                foreach (var secondInstance in objectsToCheck)
                {
                    if (!(secondInstance is ICollidable))
                    {
                        continue;
                    }

                    if (secondInstance != firstInstance &&
                        Utilities.RectanglesIntersect(
                            firstInstance.TopLeft.Col,
                            firstInstance.TopLeft.Row,
                            firstInstance.TopLeft.Col + firstInstance.Image.GetLength(1),
                            firstInstance.TopLeft.Row + firstInstance.Image.GetLength(0),
                            secondInstance.TopLeft.Col,
                            secondInstance.TopLeft.Row,
                            secondInstance.TopLeft.Col + secondInstance.Image.GetLength(1),
                            secondInstance.TopLeft.Row + secondInstance.Image.GetLength(0)))
                    {
                        ImageCollisionCheck(
                            firstInstance as ICollidable,
                            secondInstance as ICollidable);
                    }
                }
            }
        }

        private static void ImageCollisionCheck(
            ICollidable instance1, ICollidable instance2)
        {
            var collisionMask1 = (instance1 as GameObject).Image;
            var collisionMask2 = (instance2 as GameObject).Image;
            var firstInstancePosition = (instance1 as GameObject).TopLeft;
            var secondInstancePosition = (instance2 as GameObject).TopLeft;

            char cell, cellOther;

            for (int row = 0; row < collisionMask1.GetLength(0); row++)
            {
                for (int col = 0; col < collisionMask1.GetLength(1); col++)
                {
                    cell = collisionMask1[row, col];
                    if (cell == ' ')
                    {
                        continue;
                    }

                    for (int rowOther = 0; rowOther < collisionMask2.GetLength(0); rowOther++)
                    {
                        for (int colOther = 0; 
                            colOther < collisionMask2.GetLength(1); colOther++)
                        {
                            cellOther = collisionMask2[rowOther, colOther];

                            if (cellOther == ' ')
                            {
                                continue;
                            }

                            instance1.CollisionResponse(instance2);
                            // as we run each-to-each object check, we do not need
                            // to execute the instance2.collisionResponse method as it will
                            // get executed as well
                            return;
                        }
                    }
                }
            }
        }
    }
}
